package com.tanhua.dubbo.api;

import cn.itcast.mongo.Comment;
import cn.itcast.mongo.UserFocus;
import cn.itcast.mongo.Video;
import cn.itcast.tanhua.api.UserFApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class UserFApimpl implements UserFApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public boolean comm(Long userId, ObjectId id) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("followedId").is(id)
                .and("status").is(true);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public void fTest(Comment comment) {
        //1、查询动态
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        //2、向comment对象设置被评论人属性
        if(video != null) {
            comment.setPublishUserId(video.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(comment);

    }
}
